﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.Model
{
    /// <summary>
    /// 实体对象基类  唐海华从ShopSpace 中借鉴了 IEquatable
    /// </summary>
    public abstract class EntityBase : IEquatable<EntityBase>
    {
        /// <summary>
        /// Id 主键
        /// </summary>
        public virtual Guid Id { get; set; }

        /// <summary>
        /// Version 并发版本号
        /// </summary>
        public virtual int Version { get; set; }

        /// <summary>
        /// Code 编码
        /// </summary>
        public virtual string Code { get; set; }

        /// <summary>
        /// CreateTime 创建时间
        /// </summary>
        public virtual DateTime CreateTime { get; set; }

        /// <summary>
        /// UpdateTime 更新时间
        /// </summary>
        public virtual DateTime UpdateTime { get; set; }


        // 以下代码参考：ShopSpace 中 Entity 定义
        public virtual bool Equals(EntityBase obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (GetType() != obj.GetType())
                return false;
            return obj.Id == Id;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (GetType() != obj.GetType())
                return false;
            return Equals((EntityBase)obj);
        }

        public override int GetHashCode()
        {
            //return (Id.GetHashCode() * 397) ^ GetType().GetHashCode();
            return GetType().GetHashCode();     // 因为Id不为Int类型
        }

        public static bool operator ==(EntityBase left, EntityBase right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(EntityBase left, EntityBase right)
        {
            return !Equals(left, right);
        }
        // --------------------------------------------------------------

    }
}
